﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text;

namespace RSS_Reader
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
    public class Feed 
    {

        ///////////////
        //Variablen  //
        ///////////////
        private string url;
        private string channel;
        private string[][] feed_data;

        ///////////////
        //Konstruktor//
        ///////////////
        public Feed(string xml_url)
        {
            this.url = xml_url;
            var rss_raw = string.Empty;
            using (var webClient = new System.Net.WebClient())
            {
                webClient.Proxy = null;
                rss_raw = webClient.DownloadString(xml_url);
            }
            this.channel = set_channel(rss_raw);
            bool is_utf8 = false;
            if (rss_raw.Contains("utf-8") || rss_raw.Contains("UTF-8"))
            {
                is_utf8 = true;
            }
            string[] raw_items = rss_raw.Split(new String[] { "</image>", "</channel>" }, StringSplitOptions.None);
            fill_feed_data(raw_items[1], is_utf8);
        }

        ///////////////////
        //Set the Channel//
        ///////////////////
        private string set_channel(string raw_data)
        {
            string[] delimiterStrings = new string[] { "<title>", "</title>" };
            string[] delimiterURL = new string[] { "<link>", "</link>" };
            string[] words = raw_data.Split(delimiterStrings, StringSplitOptions.None);

            return words[1];
        }

        ///////////////////
        //Fill Feed Data //
        ///////////////////
        private void fill_feed_data(string raw_items, bool is_utf8)
        {
            string[] delimiterHeadline = new string[] { "<title>", "</title>" };
            string[] delimiterURL = new string[] { "<link>", "</link>" };
            string[] headlines = raw_items.Split(delimiterHeadline, StringSplitOptions.RemoveEmptyEntries);
            string[] urls = raw_items.Split(delimiterURL, StringSplitOptions.RemoveEmptyEntries);
            int amount_of_headlines = (headlines.Length - 2) / 2;
            this.feed_data = new string[amount_of_headlines][];
            for (int i = 0; i < amount_of_headlines - 1; i++)
            {
                this.feed_data[i] = new string[2];
            }

            for (int i = 0; i < amount_of_headlines - 1; i++)
            {
                //MessageBox.Show(headlines[2 * i + 1]);
                if (is_utf8)
                {
                    this.feed_data[i][0] = DecodeFromUtf8(headlines[2 * i + 1]);
                }
                else
                {
                    this.feed_data[i][0] = headlines[2 * i + 1];
                }
                this.feed_data[i][1] = urls[2 * i + 1];
            }

        }
        /// <summary>
        /// Decoder for Utf8
        /// </summary>
        /// <param name="utf8String"></param>
        /// <returns></returns>
        private string DecodeFromUtf8(string utf8String)
        {
            // copy the string as UTF-8 bytes.
            string flawless = utf8String.Replace("â€™", "'");
            byte[] utf8Bytes = new byte[flawless.Length];
            for (int i = 0; i < flawless.Length; ++i)
            {
                //Debug.Assert( 0 <= utf8String[i] && utf8String[i] <= 255, "the char must be in byte's range");
                utf8Bytes[i] = (byte)flawless[i];
            }

            return Encoding.UTF8.GetString(utf8Bytes, 0, utf8Bytes.Length);
        }

        public string get_channel()
        {
            return this.channel;
        }
        public string get_url()
        {
            return this.url;
        }
        public string[][] get_data()
        {
            return this.feed_data;
        }
        public void refresh_feed()
        {
            var rss_raw = string.Empty;
            using (var webClient = new System.Net.WebClient())
            {
                webClient.Proxy = null;
                rss_raw = webClient.DownloadString(this.url);
            }
            this.channel = set_channel(rss_raw);
            bool is_utf8 = false;
            if (rss_raw.Contains("utf-8") || rss_raw.Contains("UTF-8"))
            {
                is_utf8 = true;
            }
            string[] raw_items = rss_raw.Split(new String[] { "</image>", "</channel>" }, StringSplitOptions.None);
            fill_feed_data(raw_items[1], is_utf8);
            
        }
    }
}
